package com.collectplus.express.parcel;

import android.app.Activity;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.collectplus.express.R;
import com.collectplus.express.model.CollectingPassportBean;

import java.text.MessageFormat;
import java.util.ArrayList;

import droid.frame.activity.base.BaseAdapterExt;
import droid.frame.utils.GlideUtil;
import droid.frame.utils.lang.Str;

/**
 * Created by lidandan on 2018/3/7.
 */
public class ParcelWaitTakeAdapter extends BaseAdapterExt<CollectingPassportBean> {

    public ParcelWaitTakeAdapter(ArrayList<CollectingPassportBean> items, Activity context) {
        super(items, context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.parcel_wait_take_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.expressLogo = (ImageView) convertView.findViewById(R.id.express_logo);
            viewHolder.parcelSize = (ImageView) convertView.findViewById(R.id.parcel_size);
            viewHolder.expressCodeTv = (TextView) convertView.findViewById(R.id.express_code_tv);
            viewHolder.recipientTv = (TextView) convertView.findViewById(R.id.recipient_tv);
            viewHolder.stagingCodeTv = (TextView) convertView.findViewById(R.id.staging_code_tv);
            viewHolder.deliveryTimeTv = (TextView) convertView.findViewById(R.id.delivery_time_tv);
            viewHolder.contractPhoneTV = (TextView) convertView.findViewById(R.id.parcel_contract_phone_tv);
            viewHolder.parcelNumTV = (TextView) convertView.findViewById(R.id.parcel_num_tv);
            viewHolder.bottomLineV = convertView.findViewById(R.id.divde_line_v);
            viewHolder.parcelTagStatusIV = (ImageView) convertView.findViewById(R.id.parcel_tag_status_iv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        CollectingPassportBean item = getItem(position);
        String tipStr = "";
        if (context instanceof ParcelWaitTakenActivity) {
            tipStr = "<font color=#C6C6C6>暂存编码：{0} (请在</font><font color=#C6C6C6>{1}号</font><font color=#C6C6C6>货架上查找该包裹)</font>";
            viewHolder.parcelTagStatusIV.setVisibility(View.VISIBLE);
            if (item.getSize() == 0) {
                viewHolder.parcelSize.setImageResource(R.drawable.take_parcel_small_disable_icon);
            } else {
                viewHolder.parcelSize.setImageResource(R.drawable.take_parcel_big_disable_icon);
            }
        } else {
            tipStr = "<font color=#404040>暂存编码：{0} (请在</font><font color=#ff001f>{1}号</font><font color=#404040>货架上查找该包裹)</font>";
            viewHolder.parcelTagStatusIV.setVisibility(View.GONE);
            if (item.getSize() == 0) {
                viewHolder.parcelSize.setImageResource(R.drawable.take_parcel_small_icon);
            } else {
                viewHolder.parcelSize.setImageResource(R.drawable.take_parcel_big_icon);
            }
        }
        GlideUtil.loadImage(context, item.getExpressCompanyHeadImage(), 0, viewHolder.expressLogo);
        if (Str.isNotEmpty(item.getExpressNumber())) {
            viewHolder.expressCodeTv.setText(item.getExpressCompanyName() + "：" + item.getExpressNumber());
        } else {
            viewHolder.expressCodeTv.setText(item.getExpressCompanyName());
        }
        viewHolder.recipientTv.setText("收件人：" + item.getReceiverName());
        viewHolder.contractPhoneTV.setText("联系电话：" + item.getReceiverPhone());
        viewHolder.stagingCodeTv.setText(Html.fromHtml(MessageFormat.format(tipStr, item.getStorageCode(), item.getNumber())));
        viewHolder.stagingCodeTv.getPaint().setFakeBoldText(true);
        viewHolder.deliveryTimeTv.setText(item.getCreateTime() + " 投递至该驿站");
        viewHolder.parcelNumTV.setText((position + 1) + "");

        if (position == getItemsSize() - 1) {
            viewHolder.bottomLineV.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.bottomLineV.setVisibility(View.VISIBLE);
        }
        return convertView;
    }

    class ViewHolder {
        private ImageView expressLogo;
        private ImageView parcelSize;
        private TextView expressCodeTv;
        private TextView recipientTv;
        private TextView stagingCodeTv;
        private TextView deliveryTimeTv;
        private TextView contractPhoneTV;
        private TextView parcelNumTV;
        private View bottomLineV;
        private ImageView parcelTagStatusIV;
    }
}
